package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StMaterialBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StMaterialVo;
import com.ruoyi.ruoyiyinzheng.domain.StMaterial;
import com.ruoyi.ruoyiyinzheng.mapper.StMaterialMapper;
import com.ruoyi.ruoyiyinzheng.service.IStMaterialService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物料管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
public class StMaterialServiceImpl implements IStMaterialService {

    private final StMaterialMapper baseMapper;

    /**
     * 查询物料管理
     */
    @Override
    public StMaterialVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物料管理列表
     */
    @Override
    public TableDataInfo<StMaterialVo> queryPageList(StMaterialBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StMaterial> lqw = buildQueryWrapper(bo);
        Page<StMaterialVo> result = baseMapper.selectStMaterialPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物料管理列表
     */
    @Override
    public List<StMaterialVo> queryList(StMaterialBo bo) {
        LambdaQueryWrapper<StMaterial> lqw = buildQueryWrapper(bo);
        return baseMapper.selectStMaterialList(lqw);
    }

    private LambdaQueryWrapper<StMaterial> buildQueryWrapper(StMaterialBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StMaterial> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSeq()), StMaterial::getSeq, bo.getSeq());
        lqw.eq(bo.getCategoryId() != null, StMaterial::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), StMaterial::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StMaterial::getName, bo.getName());
        lqw.eq(bo.getSupplierId() != null, StMaterial::getSupplierId, bo.getSupplierId());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), StMaterial::getModel, bo.getModel());
        lqw.like(StringUtils.isNotBlank(bo.getUnitName()), StMaterial::getUnitName, bo.getUnitName());
        lqw.eq(StringUtils.isNotBlank(bo.getPrice()), StMaterial::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getQuality()), StMaterial::getQuality, bo.getQuality());
        lqw.eq(StringUtils.isNotBlank(bo.getEnable()), StMaterial::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增物料管理
     */
    @Override
    public Boolean insertByBo(StMaterialBo bo) {
        StMaterial add = BeanUtil.toBean(bo, StMaterial.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物料管理
     */
    @Override
    public Boolean updateByBo(StMaterialBo bo) {
        StMaterial update = BeanUtil.toBean(bo, StMaterial.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StMaterial entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除物料管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
